package com.ruoyi.recycle.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.recycle.domain.Housekeeping;
import com.ruoyi.recycle.dto.HousekeepingDTO;
import com.ruoyi.recycle.service.IHousekeepingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 家政服务Controller
 *
 * @author Wubk
 * @date 2022-05-22
 */
@RestController
@RequestMapping("/housekeeping")
@Api(tags = "家政服务")
public class HousekeepingController extends BaseController {
    @Autowired
    private IHousekeepingService housekeepingService;

    /**
     * 查询家政服务列表
     */
    @RequiresPermissions("recycle:housekeeping:list")
    @GetMapping("/list")
    public TableDataInfo list(Housekeeping housekeeping) {
        startPage();
        List<Housekeeping> list = housekeepingService.selectHousekeepingList(housekeeping);
        return getDataTable(list);
    }

    /**
     * 分页查询家政服务列表
     */
    @RequiresPermissions("recycle:housekeeping:list")
    @PostMapping("/housekeepingPageQuery")
    @ApiOperation("分页查询家政服务列表")
    public IPage<Housekeeping> housekeepingOrderPageQuery(@RequestBody HousekeepingDTO queryDTO) {

        final Housekeeping factory = new Housekeeping();
        BeanUtils.copyProperties(queryDTO, factory);
        final LambdaQueryWrapper<Housekeeping> wrapper = Wrappers.lambdaQuery(factory);
        if (queryDTO.getSearchTimeBegin() != null) {
            wrapper.ge(Housekeeping::getCreatedTime, queryDTO.getSearchTimeBegin());
        }
        if (queryDTO.getSearchTimeEnd() != null) {
            wrapper.le(Housekeeping::getCreatedTime, queryDTO.getSearchTimeEnd());
        }
        wrapper.orderByDesc(Housekeeping::getCreatedTime);
        return housekeepingService.page(new Page<Housekeeping>()
                .setCurrent(queryDTO.getCurrentPage())
                .setSize(queryDTO.getPageSize()), wrapper);
    }

    /**
     * 导出家政服务列表
     */
    @RequiresPermissions("recycle:housekeeping:export")
    @Log(title = "家政服务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation("导出家政服务列表")
    public void export(HttpServletResponse response, Housekeeping housekeeping) {
        List<Housekeeping> list = housekeepingService.selectHousekeepingList(housekeeping);
        ExcelUtil<Housekeeping> util = new ExcelUtil<Housekeeping>(Housekeeping.class);
        util.exportExcel(response, list, "家政服务数据");
    }

    /**
     * 获取家政服务详细信息
     */
    @RequiresPermissions("recycle:housekeeping:query")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取家政服务详细信息")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", required = true)
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(housekeepingService.selectHousekeepingById(id));
    }

    /**
     * 新增家政服务
     */
    @RequiresPermissions("recycle:housekeeping:add")
    @Log(title = "家政服务", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增家政服务")
    public AjaxResult add(@RequestBody Housekeeping housekeeping) {
        housekeeping.setCreatedTime(new Date());
        return toAjax(housekeepingService.insertHousekeeping(housekeeping));
    }

    /**
     * 修改家政服务
     */
    @RequiresPermissions("recycle:housekeeping:edit")
    @Log(title = "家政服务", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改家政服务")
    public AjaxResult edit(@RequestBody Housekeeping housekeeping) {
        housekeeping.setUpdatedTime(new Date());
        return toAjax(housekeepingService.updateHousekeeping(housekeeping));
    }

    /**
     * 删除家政服务
     */
    @RequiresPermissions("recycle:housekeeping:remove")
    @Log(title = "家政服务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation("删除家政服务")
    @ApiImplicitParam(name = "ids", value = "id集合", dataType = "List", required = true)
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(housekeepingService.deleteHousekeepingByIds(ids));
    }
}
